package ricky.animate
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.text.StaticText;
	import flash.utils.Dictionary;
	
	import ricky.loader.protocol.da.DaSceneConfig;
		
	/**
	 * 动画中的场景资源
	 */
	public class AnimScene
	{
		public function AnimScene(sceneName:String=null,frames:Array=null,speed:Number=NaN)
		{
			this._actionName=sceneName;
			if(frames)
				this.frames=frames;
			else
				this.frames=[];

			if(speed)
				this._fps=speed;
			else
				this._fps=defaultSpeed;
			
		}
		
		/**
		 * 计算当前帧
		 * 参数：</br>
		 * dt:Number 时间差
		 */
		public function calcFrame(dt:Number):void
		{
			if (dt < 0)
				_currFrame= 0;	
			
			_currFrame = ( dt * _fps) % _totalFrame;
//			return _currFrame;
		}
		
		private var _currFrame:int=-1;//当前帧
		
		private var _actionName:String;

		private var _url:String;
		
		private var _model:String;
		
		/**
		 * 加载场景的请求 key request.key
		 */
		public function get model():String
		{
			return _model;
		}

		/**
		 * @private
		 */
		public function set model(value:String):void
		{
			_model = value;
		}

		/**
		 * 对应的文件名。
		 */
		public function get url():String
		{
			return _url;
		}

		/**
		 * @private
		 */
		public function set url(value:String):void
		{
			_url = value;
		}

		/**
		 * 帧高度
		 */
		public function get frameHeight():Number
		{
			return _frameHeight;
		}

		/**
		 * @private
		 */
		public function set frameHeight(value:Number):void
		{
			_frameHeight = value;
		}

		/**
		 * 帧宽度
		 */
		public function get frameWidth():Number
		{
			return _frameWidth;
		}

		/**
		 * @private
		 */
		public function set frameWidth(value:Number):void
		{
			_frameWidth = value;
		}

		/**
		 * 场景名 动作名
		 * 比如播放跑步动作 sceneName="run_1" _1表示方向 顺时针 0-7
		 */
		public function get actionName():String
		{
			return _actionName;
		}

		/**
		 * @private
		 */
		public function set actionName(value:String):void
		{
			_actionName = value;
		}

		
		/**
		 * 播放速度
		 */
		public function get fps():uint
		{
			return _fps;
		}
		
		private var _fps:uint=6;
		
		/**
		 * 得到当前场景的总帧数
		 */
		public function get totalFrame():uint
		{
			return 	this._totalFrame;
		}
	
		/**
		 * 效果帧
		 */
		public function get effctFrame():int
		{
			return _effctFrame;
		}
		
		public function set effctFrame(value:int):void
		{
			_effctFrame = value;
		}

		/**
		 * 得到当前帧的位图资源
		 * i从1开始
		 */
		public function getBitMapData(i:int):BitmapData
		{
//			if(i<totalFrame && i>=0)
				return this.frames[i];// as BitmapData;
//			return null;
		}
		
		/**
		 * 克隆一个镜像
		 * 默认镜像为水平翻转
		 */
		public function cloneMatrix(newSceneName:String,matrix:Matrix=null):AnimScene
		{
				
			var mx:Matrix;
			if(matrix==null)
				mx= new Matrix(-1,0,0,1,frames[0].width,0);
			else
				mx=matrix;
			
			var matFrames:Array=new Array();
			var sbit:BitmapData;//当期原始图片
			var tarBit:BitmapData;//目标 镜像后的图片
			for(var i:int=0;i<frames.length;i++)
			{
				sbit=frames[i];
				tarBit=new BitmapData(sbit.width,sbit.height,true,0);
				tarBit.draw(sbit,mx);
				matFrames.push(tarBit);
			}			
			
			var an:AnimScene=new AnimScene(newSceneName,matFrames,this._fps)
				an.offx=this.offx;
				an.offy=this.offy;
				an._fps=this.fps;
				an.model=this.model;
				an.url=this.url;
				an.effctFrame=this.effctFrame;
				an.frameWidth=this.frameWidth;
				an.frameHeight=this.frameHeight;				
			return an;
		}

		/**
		 * 帧资源
		 */
		public function get frames():Array
		{
			return _frames;
		}

		/**
		 * @private
		 */
		public function set frames(value:Array):void
		{
			_frames = value;
			if(this._frames)
				_totalFrame=this._frames.length;
		}
		
		
		/**
		 * 得到场景数据的缓存key
		 */
		public function getKey():String
		{
			return	this._model+this._actionName;
		}
		
		private var _frameWidth:Number;
		
		private var _frameHeight:Number;
		/**
		 * 动画偏移x
		 */
		public var offx:Number=0;
		
		/**
		 * 动画偏移y
		 */
		public var offy:Number=0;
		
		private var _frames:Array;
		
		private var _totalFrame:uint=0;
		/**
		 * 效果帧
		 */
		private var _effctFrame:int=-1;

		/**
		 * 默认播放速度
		 */
		public static var defaultSpeed:uint=6;
		
		/**
		 * 从配置文件中生成场景数据
		 */
		public static function getAnimScene(scConfig:DaSceneConfig):AnimScene
		{
			var anis:AnimScene=new AnimScene(scConfig.sceneName,scConfig.frames,scConfig.speed);
			anis.effctFrame=scConfig.effctFrame;
			anis.frameWidth=scConfig.frameWidth;
			anis.frameHeight=scConfig.frameHeight;
			return anis;
		}
	
		/**
		 * 计算场景的缓存key
		 */
		public static function getAnimSceneCacheKey(fileCacheKey:String,actionName:String):String
		{
			return fileCacheKey+actionName;
		}

		public function get currFrame():int
		{
			return _currFrame;
		}

		public function set fps(value:uint):void
		{
			_fps = value;
		}


	}
}